<template>
  <el-dialog v-model="props.isShow" :before-close="handelClose" :close-on-click-modal="false">
    <el-input v-model="barCode" ref="barCodeInput" style="width: 40vw;" @keyup.enter.native="handleEnter"
      placeholder="请扫描条形码" clearable />
    <el-table :data="tables" style="width: 100%; margin-top: 20px;"  class="my-custom-table">
      <el-table-column prop="code" label="条形码" />
      <el-table-column prop="bookName" label="书名" />
      <el-table-column prop="publishTime" label="出版时间" />
    </el-table>
    <el-button type="primary" style="margin-top: 20px;display: flex;justify-content: center;" @click="applayBorrow">提交</el-button>
  </el-dialog>
</template>

<script lang="ts" setup>
import { onMounted, ref ,watch} from 'vue';
import {  confirmReceive, getBookInfoByCodeReturn, judgeCodeExist } from '../../../../../api/index'
import { ElMessage } from 'element-plus';
import { useRoute } from 'vue-router';

const barCodeInput :any = ref(null);
onMounted(() => {
  // barCodeInput.value.focus()
})
const emits = defineEmits(['confirmClose','close'])
const route = useRoute()
const props = defineProps<{ isShow: boolean, infos: any ,receiveId:any}>();
let tables = ref<any>([])
let barCode = ref<any>('')
watch(() => props.isShow, () => {
    tables.value = [];
})
const handleEnter = () => {
  if (barCode.value) {
    let codeM = barCode.value;
    barCode.value = '';
    // 查询barcode是否在tables中
    // if (tables.value.find((el: any) => el.code === codeM)) {
    //   return ElMessage.warning('code已存在');
    // } else 
    if (tables.value.length >= props.infos.borrowNum) {
      return ElMessage.error('借阅数量超过限制')
    } else {
      judgeCodeExist({receiveId:props.receiveId,barCode:codeM,status:30}).then(res=>{
        if(res.data){
          getBookInfoByCodeReturn(codeM).then((res: any) => {
            if (res.code === 200) {
            // if (res.data.drawerId == props.infos.drawId) {
              let miji = '一般'
              if (res.data.classification === '10') {
              miji = '内部'
            } else if (res.data.classification === '20') {
              miji = '秘密'
            } else if (res.data.classification === '30') {
              miji = '绝密'
            }else if (res.data.classification === '40') {
              miji = '机密'
            }
              // 查询成功
              tables.value.push({
                code: codeM,
                bookName: res.data.name,
                publishTime: res.data.publicationTime,
                miji,
                inCount: 0,
                bookId: res.data.bookId,
                orgName: res.data.name
              })
              console.log(tables);
              
            // } else {
            //   ElMessage.error('该书不在该柜子')
            // }       

        } else {
          ElMessage.error('code不存在')
        }
          })
        }else{
          return ElMessage.warning('该条码不存在');
        }
      })
      // getBookInfoByCode(codeM).then((res: any) => {
      //   if (res.code === 200) {
      //     if (res.data.status === '30'||res.data.status === '20') {
      //       if (res.data.drawerId == props.infos.drawId) {
      //         let miji = '一般'
      //         if (res.data.classification === '10') {
      //           miji = '秘密'
      //         } else if (res.data.classification === '20') {
      //           miji = '机密'
      //         } else if (res.data.classification === '30') {
      //           miji = '绝密'
      //         }
      //         // 查询成功
      //         tables.value.push({
      //           code: codeM,
      //           bookName: res.data.name,
      //           publishTime: res.data.publicationTime,
      //           miji,
      //           inCount: 0,
      //           bookId: res.data.bookId,
      //           orgName: res.data.name
      //         })
      //         console.log(tables);
              
      //       } else {
      //         ElMessage.error('该书不在该柜子')
      //       }
      //     } else {
      //       ElMessage.error('该书不在库中')
      //     }


      //   } else {
      //     ElMessage.error('code不存在')
      //   }
      // })

    }
  } else {
    // 没有barcode

  }
}

const applayBorrow = () => {
  console.log(props.infos)
  if(tables.value.length === 0){
    ElMessage.error('请先扫描图书');
    return;
  }
  // const parama = {
  //   bookId: route.params.id,
  //   receiveNum: props.infos.borrowNum,
  //   drawerId: props.infos.drawId,
  //   receiveUser: props.infos.receiveUser,
  //   receiveDay: props.infos.receiveDay,
  //   // documentUrl: props.infos.documentUrl
  // }
  // console.log(params);
  // addApply(parama).then((res: any) => {
    // if (res.code == 200) {
      const params = {
        "bookId": route.params.id,
        "receiveId": props.receiveId,
        "barCodes": tables.value.map((el: any) => el.code)
      }
      confirmReceive(params).then((ress: any) => {
        if (ress.code === 200) {
          ElMessage.success('申请成功')
          emits('confirmClose', props.infos)
        } else {
          ElMessage.error('申请失败')
        }
      })
  //   } else {
  //     ElMessage.error(res.msg);
  //   }
  // })
}

const handelClose = ()=>{
  emits('close');
}

</script>

<style lang="less" scoped></style>